__author__ = 'aesop'
#coding=utf-8

import libs.util.http_request_ssl as http_req
import json
import sys

sys.path.append("../..")
import config


__appid = config.app_id
__secret = config.app_secret


def get_openid(code):
    uri_temp = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"
    uri = uri_temp % (__appid, __secret, code)
    result = http_req(uri).do_get()

    try:
        obj = json.loads(result)
        return {"err":0, "openid":obj["openid"]}

    except:
        return {"err":-1, "description":"Wrong response data"}